package com.bci.pwtz.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.controller.admin.AdminAbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.ProductionInfo;
import com.bci.pwtz.service.ProductionInfoService;

@Controller
@RequestMapping("/productioninfo")
public class ProductionInfoController extends AdminAbstractController{
	@Autowired
private ProductionInfoService service;
	
	 @RequestMapping(value="/list",method=RequestMethod.POST)
	 @ResponseBody	
public List<ProductionInfo> list(int type){
	List<ProductionInfo> list = new ArrayList<ProductionInfo>();
	 try {
		list = service.getProList(type);
	} catch (PwtzDatabaseException e) {
		e.printStackTrace();
	}
	 return list;
}
	 @RequestMapping(value="/load",method=RequestMethod.POST)
	 @ResponseBody
	 public ProductionInfo load(int id){
		 ProductionInfo info = new ProductionInfo();
		 try {
			info = service.load(id);
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		}
		 return info;
	 }
	 @RequestMapping(value="/add",method=RequestMethod.POST)
	 @ResponseBody
	 public int add(String data){
		 Map<String, String> map = new HashMap<String, String>();
		 int result = 0;
		 ProductionInfo pro = new ProductionInfo();
		 try {
			//data=java.net.URLDecoder.decode(data,"UTF-8");
         String [] list=decodeURL(data.split("&"));
         for(int i=0;i<list.length;i++){
             String[] array=list[i].split("=");
             if(array.length==1){
                 map.put(array[0], "");
             }else{
                 map.put(array[0], array[1]);
             }
         }
			 pro.setDownloadurl(map.get("downloadurl"));
			 pro.setLanguage(map.get("language"));
			 pro.setSize(map.get("size"));
			 pro.setStatus(1);
			 pro.setSystem(map.get("system"));
			 pro.setType(Integer.parseInt(map.get("type")));
			 pro.setUpdatedesc(map.get("updatedesc"));
			 pro.setUpdatetime(PwtzUtils.parseDate(map.get("updatetime"),"yyyy-MM-dd"));
			 pro.setVersion(map.get("version"));
			 result = service.insert(pro);
			 setLog(4, "添加产品【产品系统】【"+pro.getSystem()+"】成功", getUserId());
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		 return result;
	 }
	 @RequestMapping(value="/update",method=RequestMethod.POST)
	 @ResponseBody
	public int update(String data){

		 Map<String, String> map = new HashMap<String, String>();
		 int result = 0;
		 try {
			//data=java.net.URLDecoder.decode(data,"UTF-8");
        String [] list=decodeURL(data.split("&"));
        for(int i=0;i<list.length;i++){
            String[] array=list[i].split("=");
            if(array.length==1){
                map.put(array[0], "");
            }else{
                map.put(array[0], array[1]);
            }
        }
        ProductionInfo pro = service.load(Integer.parseInt(map.get("id")));
			 pro.setDownloadurl(map.get("downloadurl"));
			 pro.setLanguage(map.get("language"));
			 pro.setSize(map.get("size"));
			 pro.setSystem(map.get("system"));
			 pro.setType(Integer.parseInt(map.get("type")));
			 pro.setUpdatedesc(map.get("updatedesc"));
			 pro.setUpdatetime(PwtzUtils.parseDate(map.get("updatetime"),"yyyy-MM-dd"));
			 pro.setVersion(map.get("version"));
			 result = service.update(pro);
			 setLog(4, "修改产品【产品系统】【"+pro.getSystem()+"】成功", getUserId());
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		 return result;
	 
	}
	 
	 
	 @RequestMapping(value="/delete",method=RequestMethod.POST)
	 @ResponseBody
	 
	 public int delete(int id){
		 int result = 0;
		try {
			ProductionInfo info = service.load(id);
			result =  service.deleleById(id);
			 setLog(4, "删除产品【产品系统】【"+info.getSystem()+"】成功", getUserId());
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		}
		return result;
	 }
}
